package com.example.imgup;
import java.util.ArrayList;
import java.util.List;

public class VOCXMLGenerator {

    private String fileName;
    private float imageWidth;
    private float imageHeight;
    private List<Annotation> annotations;

    public VOCXMLGenerator(String fileName, float imageWidth, float imageHeight) {
        this.fileName = fileName;
        this.imageWidth = imageWidth;
        this.imageHeight = imageHeight;
        this.annotations = new ArrayList<>();
    }

    public void addBoundingBox(String label, int left, int top, int right, int bottom) {
        annotations.add(new Annotation(label, left, top, right, bottom));
    }

    public String generateXML() {
        // 生成XML文件的逻辑
        StringBuilder xmlBuilder = new StringBuilder();
        xmlBuilder.append("<annotation>\n");
        xmlBuilder.append("  <filename>").append(fileName).append("</filename>\n");
        xmlBuilder.append("  <size>\n");
        xmlBuilder.append("    <width>").append(imageWidth).append("</width>\n");
        xmlBuilder.append("    <height>").append(imageHeight).append("</height>\n");
        xmlBuilder.append("  </size>\n");

        // 添加标注框信息
        for (Annotation annotation : annotations) {
            xmlBuilder.append("  <object>\n");
            xmlBuilder.append("    <name>").append(annotation.label).append("</name>\n");
            xmlBuilder.append("    <bndbox>\n");
            xmlBuilder.append("      <xmin>").append(annotation.left).append("</xmin>\n");
            xmlBuilder.append("      <ymin>").append(annotation.top).append("</ymin>\n");
            xmlBuilder.append("      <xmax>").append(annotation.right).append("</xmax>\n");
            xmlBuilder.append("      <ymax>").append(annotation.bottom).append("</ymax>\n");
            xmlBuilder.append("    </bndbox>\n");
            xmlBuilder.append("  </object>\n");
        }

        xmlBuilder.append("</annotation>");

        return xmlBuilder.toString();
    }

    private static class Annotation {
        private String label;
        private int left;
        private int top;
        private int right;
        private int bottom;

        public Annotation(String label, int left, int top, int right, int bottom) {
            this.label = label;
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }
    }
}